package org.luckyjourney.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.List;
import java.util.Map;

/**
 * @author 飒飒
 * @ClassName PttsDemo
 * @description: TODO
 * @date 2024年03月16日
 * @version: 1.0
 */
public class PttsDemo {
    //域名
    private static final String DOMAIN = "nls-measure.cn-shanghai.aliyuncs.com";
    // API版本
    private static final String API_VERSION = "2019-09-05";

    private static String ALIYUN_AKID="LTAIZkuwP3heevhN";
    private static String ALIYUN_AKKEY="D8p1QkmsgDyk5r91ym4K5lbScBus1p";

    private static IAcsClient client;

    static {
        // 创建DefaultAcsClient实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(
                "",
                ALIYUN_AKID,
                ALIYUN_AKKEY);
        client = new DefaultAcsClient(profile);
    }

    /**
     * 获取需要朗读的文本
     * @param scenario
     * @return
     */
    public static String getText(String scenario){
        //Step1: 获取需要朗读的文本
        CommonRequest getDemonstrationRequest = buildRequest("GetDemonstrationForCustomizedVoice");
        getDemonstrationRequest.putQueryParameter("Scenario", scenario);
        String getDemonstrationResponse = sendRequest(getDemonstrationRequest);
        System.out.println("|获取需要朗读的内容|response=" + getDemonstrationResponse);
        return getDemonstrationResponse;
    }

    /**
     * 采集用户朗读的音频，进行音频检测
     * @param voiceName
     * @param scenario
     * @param urls
     * @throws InterruptedException
     */
    public static String synthesize( String voiceName, String scenario,String urls,int AudioId,String gender ) throws InterruptedException {
        //Step2: 采集用户朗读的音频，进行音频检测
            String audioUrl = audioRecordUrlPrefix + urls;
            CommonRequest audioDetectRequest = buildRequest("CustomizedVoiceAudioDetect");
            audioDetectRequest.putQueryParameter("Scenario", scenario);//场景
            audioDetectRequest.putQueryParameter("VoiceName", voiceName);
            audioDetectRequest.putQueryParameter("RecordUrl", audioUrl);
            audioDetectRequest.putQueryParameter("AudioRecordId", String.valueOf(AudioId));
            String audioDetectResponse = sendRequest(audioDetectRequest);
            System.out.println("|音频检测|[" + urls + "]response=" + audioDetectResponse);

        //Step3: 20句音频检测完成后，提交训练
        CommonRequest submitTrainRequest = buildRequest("SubmitCustomizedVoice");
        submitTrainRequest.putQueryParameter("VoiceName", voiceName);
        submitTrainRequest.putQueryParameter("Gender", gender);//性别
        submitTrainRequest.putQueryParameter("Scenario", scenario);

        String submitTrainResponse = sendRequest(submitTrainRequest);
        System.out.println("|提交训练|response=" + submitTrainResponse);

        //Step4: 轮询训练结果

        CommonRequest queryTrainResultRequest = buildRequest("ListCustomizedVoice");
        queryTrainResultRequest.putQueryParameter("VoiceName", voiceName);
        String queryTrainResultResponse = sendRequest(queryTrainResultRequest);
        System.out.println("|查询训练结果|response=" + queryTrainResultResponse);
        return queryTrainResultResponse;

}
    public static void main(String[] args) throws InterruptedException {
        //20句音频，此处为了方便演示接口的调用，提前准备好了音频的url，在您集成时，可以在用户录制的过程中，将音频进行存储
        String[] urls = {"viwf/1.wav", "dycw/2.wav", "dopl/3.wav", "anfd/4.wav", "cyoy/5.wav", "dsjw/6.wav",
                "vevd/7.wav", "ulno/8.wav", "kwlw/9.wav", "lafu/10.wav", "uozh/11.wav", "gdpp/12.wav", "lisa/13.wav",
                "bmvv/14.wav", "ijzx/15.wav", "kdla/16.wav", "govf/17.wav", "kcid/18.wav", "srdx/19.wav", "stol/20.wav"};

        //训练的基本信息，voiceName请替换成您自己的命名
        String voiceName = "示例voice";//声音名称
        String scenario = "story";//场景
        String gender = "female";//性别

        //Step1: 获取需要朗读的文本
        CommonRequest getDemonstrationRequest = buildRequest("GetDemonstrationForCustomizedVoice");
        getDemonstrationRequest.putQueryParameter("Scenario", scenario);
        String getDemonstrationResponse = sendRequest(getDemonstrationRequest);
        System.out.println("|获取需要朗读的内容|response=" + getDemonstrationResponse);

        //Step2: 采集用户朗读的音频，进行音频检测
//        for (int i = 1; i <= 20; i++) {
//            String audioUrl = audioRecordUrlPrefix + urls[i - 1];
            String audioUrl = audioRecordUrlPrefix + urls[0];
            CommonRequest audioDetectRequest = buildRequest("CustomizedVoiceAudioDetect");
            audioDetectRequest.putQueryParameter("Scenario", scenario);
            audioDetectRequest.putQueryParameter("VoiceName", voiceName);
            audioDetectRequest.putQueryParameter("RecordUrl", audioUrl);
            audioDetectRequest.putQueryParameter("AudioRecordId", String.valueOf(1));
            String audioDetectResponse = sendRequest(audioDetectRequest);
            System.out.println("|音频检测|[" + 0 + "]response=" + audioDetectResponse);
//        }

        //Step3: 20句音频检测完成后，提交训练
        CommonRequest submitTrainRequest = buildRequest("SubmitCustomizedVoice");
        submitTrainRequest.putQueryParameter("VoiceName", voiceName);
        submitTrainRequest.putQueryParameter("Gender", gender);
        submitTrainRequest.putQueryParameter("Scenario", scenario);

        String submitTrainResponse = sendRequest(submitTrainRequest);
        System.out.println("|提交训练|response=" + submitTrainResponse);

        //Step4: 轮询训练结果

        CommonRequest queryTrainResultRequest = buildRequest("ListCustomizedVoice");
        queryTrainResultRequest.putQueryParameter("VoiceName", voiceName);
        String queryTrainResultResponse = sendRequest(queryTrainResultRequest);
        System.out.println("|查询训练结果|response=" + queryTrainResultResponse);
        Map mapObj = JSONObject.parseObject(queryTrainResultResponse,Map.class);
       /* Object data = mapObj.get("Data");
        //获取data中的ModelId
        Map map = (Map) ((List) data).get(0);
        map.get("Status");
        Object modelId = map.get("ModelId");
        System.out.printf("modelId"+modelId);*/
    }

    private static String audioRecordUrlPrefix
            = "http://8.142.120.232:9107";

    private static CommonRequest buildRequest(String popApiName) {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(API_VERSION);
        request.setSysAction(popApiName);
        request.setSysProtocol(ProtocolType.HTTPS);
        return request;
    }

    private static String sendRequest(CommonRequest request) {
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
